package com.compass.doctor.ui.home.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.compass.doctor.databinding.FragmentRoutineDrugBinding
import com.compass.doctor.ui.home.adapter.RoutineDrugAdapter
import com.compass.doctor.ui.home.viewmodel.DrugViewModel
import com.compass.doctor.base.BaseVBFragment
import com.compass.framework.ext.dividerGridSpace
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * Description： 常用药品
 * Date:2023/12/7 10:13
 */
class RoutineDrugFragment : BaseVBFragment<FragmentRoutineDrugBinding>(), OnRefreshListener,
    OnLoadMoreListener {
    private var mPage = 0
    private val drugViewModel: DrugViewModel by activityViewModels()
    private lateinit var routineDrugAdapter: RoutineDrugAdapter
    override fun initView(view: View, savedInstanceState: Bundle?) {
        routineDrugAdapter = RoutineDrugAdapter()

        mBinding?.refreshLayout?.apply {
            setEnableRefresh(true)
            setEnableLoadMore(true)
            setOnRefreshListener(this@RoutineDrugFragment)
            setOnLoadMoreListener(this@RoutineDrugFragment)
        }
        mBinding?.recyclerView?.apply {
            layoutManager = GridLayoutManager(requireContext(), 2)
            adapter = routineDrugAdapter
            dividerGridSpace(2, 1.0f, true)
        }

        routineDrugAdapter.onItemClickListener = { view: View, position: Int ->
            showToast("药品详情")
        }
    }

    override fun initData() {
        super.initData()
        getRoutineDrugList()
    }

    private fun getRoutineDrugList() {

//        drugViewModel.getPharmacyInfo()
//        drugViewModel.categoryItemLiveData.observe(this) {
//            dismissLoading()
//            if (mPage == 0) {
//                routineDrugAdapter.setData(it)
//                mBinding?.refreshLayout?.finishRefresh()
//                mBinding?.viewEmpty?.visibility=View.GONE
//            } else {
//                routineDrugAdapter.addAll(it)
//                mBinding?.refreshLayout?.finishLoadMore()
//            }
//
//        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mPage = 0
        getRoutineDrugList()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPage++
        getRoutineDrugList()
    }
}